package com.example.dbg.common.utils;


import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @user DBG_ZXX
 * @date 2018/10/26 17:33
 * @desc
 **/
public class DBGDateUtils {
    /**
     * 格式化的样式
     */
    private List<String> formatPatternList = new ArrayList<String>();

    /**
     * Logger for this class
     */
    private static final Logger logger = LoggerFactory.getLogger(DBGDateUtils.class);

    private static DBGDateUtils DBGDateUtils = null;

    public static void main(String[] args) {
//        Date date = DBGDateUtils.getInstance().formatDate("2009-11-10");
//        System.out.println(date);
        String dateStr = DBGDateUtils.getInstance().getDate2Str(new Date(),"yyyy-MM-dd");
        System.out.println(dateStr);
    }

    /**
     * 单例
     *
     * @return DateUtil
     */
    public static synchronized DBGDateUtils getInstance() {
        if (DBGDateUtils == null)
            DBGDateUtils = new DBGDateUtils();
        return DBGDateUtils;
    }

    /**
     * 格式化日期
     *
     * @param str
     * @return Date
     */
    public Date formatDate(String str) {
        if (str == null) {
            if (logger.isInfoEnabled()) {
                logger.info("Date must not be null");
            }
            return new Date();
        }
        try {
            SimpleDateFormat format = null;
            ParsePosition pos = new ParsePosition(0);
            for (int i = 0; i < formatPatternList.size(); i++) {
                if (i == 0) {
                    format = new SimpleDateFormat(formatPatternList.get(0));
                }
                else {
                    format.applyPattern(formatPatternList.get(i));
                }
                pos.setIndex(0);
                Date date = format.parse(str, pos);
                if (date != null && pos.getIndex() == str.length()) {
                    return date;
                }
            }
        }
        catch (Exception e) {
            logger.error("parser date occur error." + e);
        }
        return new Date();
    }

    /**
     * @desc: 判断时间为系统当天时间
     * @param date
     * @retrun:  boolean
     * @Date:    2018/10/29 22:41
     */
    public boolean isToday(Date date){
        return this.getDate2Str(new Date(),null).equals(this.getDate2Str(date,null))? true:false;
    }

    /**
     * @desc: 判断两个日期是否为同一天
     * @param date1
     * @param date2
     * @retrun:  boolean
     * @Date:    2018/10/29 22:15
     */
    public boolean isEquelOfDate(Date date1 , Date date2){
        return this.getDate2Str(date1,null).equals(this.getDate2Str(date2,null))? true:false;
    }

    /**
     * @desc: Date 转 String
     * @param date
     * @param dateFormate
     * @retrun:  java.lang.String
     * @Date:    2018/10/29 22:09
     */
    public String getDate2Str(Date date , String dateFormate){
        if(StringUtils.checkValNull(dateFormate)){
            dateFormate = "yyyy-MM-dd";
        }
        String dateStr = DateFormatUtils.format(date,dateFormate);
        return dateStr;
    }

    public List<String> getFormatPatternList() {
        return formatPatternList;
    }

    public void addFormatPattern(String formatPattern) {
        formatPatternList.add(formatPattern);
    }
    public void addFormatPatterns(Collection formatPatterns) {
        formatPatternList.addAll(formatPatterns);
    }

    public boolean removeFormatPattern(String formatPattern) {
        return formatPatternList.remove(formatPattern);
    }

    public void removeAllFormatPattern() {
        formatPatternList.clear();
    }

    private DBGDateUtils() {
        /**
         * 初始一些日期样式
         */
        formatPatternList.add("yyyyMMdd HHmmss");
        formatPatternList.add("yyyyMMdd HHmm");
        formatPatternList.add("yyyyMMdd HH");
        formatPatternList.add("yyyyMMdd");
        formatPatternList.add("yyyyMM");
        formatPatternList.add("yyyy/MM/dd HH:mm:ss");
        formatPatternList.add("yyyy/MM/dd HH:mm");
        formatPatternList.add("yyyy/MM/dd HH");
        formatPatternList.add("yyyy/MM/dd");
        formatPatternList.add("yyyy/MM");
        formatPatternList.add("yyyy");
        // formatPatternList.add("MM/dd");
        formatPatternList.add("yyyy/MM/dd HH/mm/ss");
        formatPatternList.add("yyyy/MM/dd HH/mm");
        formatPatternList.add("yyyy-MM-dd HH:mm:ss");
        formatPatternList.add("yyyy-MM-dd HH:mm");
        formatPatternList.add("yyyy-MM-dd HH");
        formatPatternList.add("yyyy-MM-dd");
        formatPatternList.add("yyyy-MM");
        // formatPatternList.add("MM-dd");
        formatPatternList.add("yyyy-MM-dd HH-mm-ss");
        formatPatternList.add("yyyy-MM-dd HH-mm");
        formatPatternList.add("yyyy.MM.dd HH:mm:ss");
        formatPatternList.add("yyyy.MM.dd HH:mm");
        formatPatternList.add("yyyy.MM.dd HH");
        formatPatternList.add("yyyy.MM.dd");
        formatPatternList.add("yyyy.MM");
        // formatPatternList.add("MM.dd");
        formatPatternList.add("yyyy.MM.dd HH.mm.ss");
        formatPatternList.add("yyyy.MM.dd HH.mm");
        // formatPatternList.add("HH:mm:ss");
        // formatPatternList.add("HH:mm");
        formatPatternList.add("yyyyMMdd HHmmss");
    }
}
